package org.scratchcrew.domain;

import javax.persistence.AttributeOverride;
import javax.persistence.AttributeOverrides;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.Lob;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;

/**
 * Versioned Entity which contains dynamic data for a single {@link Scratch}.
 * 
 * @author tsachev
 * 
 */
@Entity
@Table(
		name = "t_scratch_data", 
		uniqueConstraints = { 
				@UniqueConstraint(columnNames = {"scratch_id", "version"}) 
				}
		)
@AttributeOverrides(value = { @AttributeOverride(column = @Column(name = "scratch_data_id"), name = "id") })
public class ScratchData extends VersionedEntity implements SoftDeletable {
	public static final String DELETED_SCRATCH_DATA = "DELETED";
	@Lob
	@Column(name = "scratch_data")
	private String scratchData;

	@OneToOne(fetch = FetchType.LAZY)
	@JoinColumn(nullable = false, name = "scratch_id")
	private Scratch scratch;

	public String getScratchData() {
		return scratchData;
	}
	@Override
	public boolean isDeleted() {
		return DELETED_SCRATCH_DATA.equals(scratchData);
	}
	@Override
	public void markAsDeleted() {
		this.scratchData = DELETED_SCRATCH_DATA;
	}

	public void setScratchData(String scratchData) {
		this.scratchData = scratchData;
	}

	public Scratch getScratch() {
		return scratch;
	}

	public void setScratch(Scratch scratch) {
		this.scratch = scratch;
	}

}
